<template>
	<view class="content">
		<view class="dynamics-wrap">
			<view class="dynamics-item">
				<view class="user-wrap">
					<image class="avatar" :src="getAvatarUrl(dynamicsInfo.user.avatar)" mode="aspectFill" @click="openUserDetails(dynamicsInfo.user)"></image>
					<view class="user-info" @click="openUserDetails(dynamicsInfo.user)">
						<view class="name-wrap">
							<text class="name">{{dynamicsInfo.user.name}}</text>
							<view :class="['sex-wrap', 'sex' + dynamicsInfo.user.sex]">
								<image class="sex-icon" :src="'/static/img/sex' + dynamicsInfo.user.sex + '.png'" mode=""></image>
								<text class="age">{{dynamicsInfo.user.age}}</text>
							</view>
						</view>
						<view class="time">{{dynamicsInfo.created_at | timeFormat}}</view>
					</view>
				</view>
				<view class="dynamics-content">
					<view class="content">
						<text v-if="!!dynamicsInfo.description" class="desc">{{dynamicsInfo.description}}</text>
						<text class="topic" @click="openDynamicsTopic">#{{dynamicsInfo.topic.name}}#</text>
					</view>
					<view class="media-wrap">
						<view v-if="dynamicsInfo.type == 1" class="img" v-for="(media, j) in dynamicsInfo.files" :key="media.id" @click="previewImage(dynamicsInfo.files, j)">
							<image :src="media.file + '?imageView2/1/w/300/h/300'" mode=""></image>
						</view>
						<view v-if="dynamicsInfo.type == 2" class="video" v-for="(media, k) in dynamicsInfo.files" :key="media.id" @click="openDynamicsVideo(media)">
							<image class="cover-image" :src="media.file + '?vframe/jpg/offset/1'" mode="aspectFill"></image>
							<image class="player-icon" src="/static/img/player_icon.png" mode=""></image>
						</view>
					</view>
				</view>
				<view class="dynamics-menu">
					<view class="menu-item" @click="commentFocus">
						<image class="menu-icon" src="/static/img/comment.png" mode=""></image>
						<text>{{dynamicsInfo.comment_size}}</text>
					</view>
					<view class="menu-item" @click="like(dynamicsInfo)">
						<image class="menu-icon" :src="'/static/img/like'+ is_like +'.png'" mode=""></image>
						<text>{{dynamicsInfo.like_size}}</text>
					</view>
					<view class="menu-item"></view>
					<view class="menu-item"></view>
				</view>
			</view>
			<view class="comment-list">
				<view class="comment-total">
					<text>最新评论（{{dynamicsInfo.comment_size}}）</text>
				</view>
				<block v-if="list.length > 0">
					<view class="comment-item" v-for="item in list" :key="item.id">
						<image class="comment-user-avatar" :src="getAvatarUrl(item.user.avatar)" mode="aspectFill" @click="openUserDetails(item.user)"></image>
						<view class="user-wrap">
							<view class="name-wrap">
								<text class="name">{{item.user.name}}</text>
								<view :class="['sex-wrap', 'sex' + item.user.sex]">
									<image class="sex-icon" :src="'/static/img/sex' + item.user.sex + '.png'" mode=""></image>
									<text class="age">{{item.user.age}}</text>
								</view>
							</view>
							<view class="comment-content">
								<text>{{item.description}}</text>
							</view>
						</view>
						<view class="time">
							<text>{{item.created_at | timeFormat}}</text>
						</view>
					</view>
				</block>
				<view v-else class="empty-wrap">
					<image class="empty-icon" src="/static/img/empty.png" mode=""></image>
					<text class="empty-text">暂无评论</text>
				</view>
			</view>
			<uni-load-more :status="status" :content-text="contentText"></uni-load-more>
		</view>
		<view class="comment-wrap">
			<input class="comment-input" v-model="comment" :adjust-position="false" :focus="focus" @blur="commentBlur" type="text" :placeholder="'回复' + dynamicsInfo.user.name">
			<button class="comment-btn" type="primary" size="mini" @click="submitComment">评论</button>
		</view>
	</view>
</template>

<script>
	import util from "@/common/util.js"
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue"
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				id: '',
				myInfo: {},
				dynamicsInfo: {
					description: '',
					type: 1,
					files: [],
					comment_size: 0,
					like_size: 0,
					created_at: 0,
					user: {
						id: '',
						name: '',
						avatar: '',
						sex: '',
						age: '',
					},
					topic: {
						name: ''
					}
				},
				list: [],
				is_like: 0,
				comment: '',
				focus: false,
				page: 1,
				status: 'more',
				contentText: {
					contentdown: '加载更多',
					contentrefresh: '加载中',
					contentnomore: '木有了~'
				},
			}
		},
		onLoad(options) {
			this.id = options.id
			try {
				const myInfo = uni.getStorageSync('myInfo');
				if (myInfo) {
					this.myInfo = myInfo;
				}
			} catch (e) {
				console.log(e)
			}
		},
		onReady() {
			this.getDynamicInfo()
		},
		onPullDownRefresh() {
			this.page = 1;
			setTimeout(() => {
				this.getDynamicInfo();
			}, 500)
		},
		onReachBottom() {
			this.status = 'loading';
			this.getDynamicInfo();
		},
		onBackPress(e) {
			uni.hideKeyboard();
		},
		methods: {
			getDynamicInfo: function () {
				uni.showLoading({
					title: ''
				});
				this.$api.api.ajaxData({
					method: 'V1.DynamicInfo',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					did: this.id,
					page: this.page
				}, res => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					if (res.status) {
						if (this.page == 1) {
							this.dynamicsInfo = res.data.dynamic
							this.list = res.data.list
							this.is_like = res.data.is_like
							this.page++
							this.status = 'more'
							uni.setNavigationBarTitle({
								title: this.dynamicsInfo.user.name
							})
						} else {
							if (res.data.list.length > 0) {
								for (let i = 0; i < res.data.list.length; i++) {
									this.list.push(res.data.list[i])
								}
								this.page++
								this.status = 'more'
							} else {
								this.status = 'noMore'
							}
						}
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			openUserDetails: function (item) {
				let user = {
					id: item.id,
					name: item.name,
					avatar: item.avatar
				}
				uni.navigateTo({
					url: '../home/user_home?user=' + JSON.stringify(user)
				})
			},
			
			openDynamicsTopic: function () {
				uni.navigateTo({
					url: 'dynamics_list?type=topic&tid=' + this.dynamicsInfo.topic.id + '&title=' + this.dynamicsInfo.topic.name
				})
			},
			
			like: function (item) {
				let type = ''
				if (this.is_like == 0) {
					type = 'like'
				}
				if (this.is_like == 1) {
					type = 'other'
				}
				uni.showLoading({
					title: ''
				});
				this.$api.api.ajaxData({
					method: 'V1.DynamicLike',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					did: item.id,
					type: type,
				}, res => {
					uni.hideLoading();
					if (res.status) {
						if (type == 'like') {
							uni.showToast({
								title: '点赞成功',
								position: 'bottom',
								icon: 'none'
							});
							this.is_like = 1
							this.dynamicsInfo.like_size++
						}
						if (type == 'other') {
							uni.showToast({
								title: '取消点赞',
								position: 'bottom',
								icon: 'none'
							});
							this.is_like = 0
							this.dynamicsInfo.like_size--
						}
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			previewImage: function (list, index) {
				let templist = [];
				for (let i = 0; i < list.length; i++) {
					templist.push(list[i].file);
				}
				uni.previewImage({
					current: index,
					urls: templist,
			  });
			},
			
			openDynamicsVideo: function (item) {
				let media = {
					file: item.file
				}
				uni.navigateTo({
					url: 'video?media=' + encodeURIComponent(JSON.stringify(media))
				})
			},
			
			commentFocus: function () {
				this.focus = true
			},
			
			commentBlur: function () {
				this.focus = false
			},
			
			submitComment: function () {
				uni.showLoading({
					title: ''
				});
				this.$api.api.ajaxData({
					method: 'V1.DynamicComment',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					did: this.id,
					description: this.comment,
				}, res => {
					uni.hideLoading();
					if (res.status) {
						this.dynamicsInfo.comment_size++;
						this.list.unshift({
							description: this.comment,
							created_at: new Date().getTime(),
							user: {
								id: this.myInfo.id,
								name: this.myInfo.name,
								avatar: this.myInfo.avatar,
								sex: this.myInfo.sex,
								age: this.myInfo.age
							}
						})
						this.comment = '';
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			getAvatarUrl: function (value) {
				return value.indexOf(this.QINIUURL) == -1 ? value : value + '?imageView2/1/w/100/h/100'
			},
		},
		filters: {
			timeFormat: function (value) {
				let now = new Date().getTime();
				let old = value
				let	oldTime = 0
				if (typeof value === 'number') {
					oldTime = old*1000;
				}
				if (typeof value === 'string') {
					oldTime = new Date(value.replace(/-/g, '/')).getTime();
				}
				return util.dateUtils.humanize(now - oldTime);
			}
		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	.dynamics-wrap {
		padding: 1rpx 0;
		margin-bottom: 120rpx;
	}
	.dynamics-item {
		background-color: #FFFFFF;
		padding: 20rpx;
		margin-bottom: 10rpx;
		.user-wrap {
			display: flex;
			justify-content: center;
			align-items: center;
			.avatar {
				width: 120rpx;
				height: 120rpx;
				border-radius: 120rpx;
				background-color: $image-bg-color;
			}
			.user-info {
				flex: 1;
				margin: 0 10rpx;
				.name-wrap {
					display: flex;
					justify-content: flex-start;
					align-items: center;
				}
				.name {
					max-width: 360rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-size: 36rpx;
					color: #333;
					margin-right: 10rpx;
				}
				.sex-wrap {
					display: flex;
					justify-content: center;
					align-items: center;
					line-height: 28rpx;
					padding: 5rpx 10rpx;
					border-radius: 28rpx;
					&.sex1 {
						background: #83d7ff;
					}
					&.sex2 {
						background: #fe9ab7;
					}
					.sex-icon {
						width: 28rpx;
						height: 28rpx;
					}
					.age {
						font-size: 28rpx;
						color: #fff;
						margin-left: 5rpx;
					}
				}
				.time {
					font-size: 30rpx;
					color: #666666;
				}
			}
		}
		.dynamics-content {
			margin-left: 110rpx;
			.content {
				.desc {
					font-size: 32rpx;
					color: #333;
					line-height: 40rpx;
					margin-right: 10rpx;
				}
				.topic {
					font-size: 32rpx;
					color: #007AFF;
					line-height: 40rpx;
				}
			}
			.media-wrap {
				display: flex;
				flex-wrap: wrap;
				.img {
					width: 200rpx;
					height: 200rpx;
					padding: 5rpx;
					box-sizing: border-box;
					image {
						display: block;
						width: 190rpx;
						height: 190rpx;
						border-radius: 10rpx;
					}
				}
				.video {
					width: 600rpx;
					height: 360rpx;
					padding: 5rpx;
					box-sizing: border-box;
					position: relative;
					.cover-image {
						display: block;
						width: 590rpx;
						height: 350rpx;
						border-radius: 10rpx;
					}
					.player-icon {
						width: 50rpx;
						height: 50rpx;
						position: absolute;
						left: 50%;
						top: 50%;
						margin-left: -25rpx;
						margin-right: -25rpx;
					}
				}
			}
		}
		.dynamics-menu {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-left: 110rpx;
			.menu-item {
				display: flex;
				align-items: center;
				padding: 20rpx 0;
				margin-right: 50rpx;
				.menu-icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}
				text {
					font-size: 36rpx;
					color: #666;
				}
			}
		}
	}
	.comment-list {
		background-color: #FFFFFF;
		.comment-total {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			height: 80rpx;
			padding: 0 20rpx;
		}
		.comment-item {
			display: flex;
			justify-content: flex-start;
			align-items: flex-start;
			padding: 20rpx;
			& + .comment-item {
				border-top: 1px solid $border-light-color;
			}
			.comment-user-avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 80rpx;
				background-color: $image-bg-color;
			}
			.user-wrap {
				flex: 1;
				margin: 0 10rpx;
				.name-wrap {
					display: flex;
					justify-content: flex-start;
					align-items: center;
				}
				.name {
					font-size: 24rpx;
					color: $title-color;
					margin-right: 10rpx;
				}
				.sex-wrap {
					display: flex;
					justify-content: center;
					align-items: center;
					line-height: 24rpx;
					padding: 0 10rpx;
					border-radius: 24rpx;
					&.sex1 {
						background: #83d7ff;
					}
					&.sex2 {
						background: #fe9ab7;
					}
					.sex-icon {
						width: 15rpx;
						height: 15rpx;
					}
					.age {
						font-size: 18rpx;
						color: #fff;
						margin-left: 2rpx;
					}
				}
			}
			.comment-content {
				text {
					font-size: 24rpx;
					color: $text-color;
				}
			}
			.time {
				text {
					font-size: 24rpx;
					color: $text-color;
				}
			}
		}
	}
	.comment-wrap {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		width: 100%;
		box-sizing: border-box;
		padding: 20rpx 30rpx;
		background-color: #efefef;
		position: fixed;
		left: 0;
		bottom: 0;
		.comment-input {
			flex: 1;
			height: 80rpx;
			line-height: 80rpx;
			padding: 0 30rpx;
			background-color: #FFFFFF;
			border-radius: 80rpx;
		}
		.comment-btn {
			margin-left: 20rpx;
		}
	}
</style>
